package com.heitian.ssm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.heitian.ssm.Constants.WebConstants;
import com.heitian.ssm.model.User;
import com.heitian.ssm.service.UserService;
import com.heitian.ssm.utils.FastJsonUtils;
import com.heitian.ssm.utils.JsonResultUtils;
import com.heitian.ssm.utils.MailUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by mishuai on 2018/4/19.
 */

@Controller
@RequestMapping("/user")
public class UserController {
   // private final Logger logger = new Logger(UserController.class);
    private Logger log = Logger.getLogger(UserController.class);
    @Resource
    private UserService userService;

    @RequestMapping("/defaultIndex")
   public String defaultIndex(){
      return "Default";
   }

    @RequestMapping("/showUser")
    @ResponseBody
    public void showUser(HttpServletRequest request,HttpServletResponse response, Model model){
        log.info("查询所有用户信息");
      /*  request=null;
        request.getCookies();*/
        List<User> userList = userService.getAllUser();
        log.info(JSON.toJSONString(userList));
       // JSON.parseArray(userList.toString());
      //  FastJsonUtils.toJSONObject(userList.toString());
      /*  try {
            MailUtil.send_mail(WebConstants.MAIL,"您已选择澡位，请按时间赴约哟！");
        } catch (MessagingException e) {
            e.printStackTrace();
        }*/
        JsonResultUtils.ajaxReturn(response,FastJsonUtils.toJSONString(userList));
       // model.addAttribute("userList",userList);
       // return "showUser";
    }


    @RequestMapping("/index")
    public  String addLogin(HttpServletRequest request, HttpServletResponse response, Model model, String userName){
        String name=null;
        String value=null;

        Cookie[] cookies = request.getCookies();
        if(request.getCookies() !=null){
            for(int i = 0 ; i < cookies.length ; i++){
                 name = cookies[i].getName() ;
                 if(name.equals(userName)){
                     value = cookies[i].getValue() ;
                     User user1=FastJsonUtils.toBean(value,User.class);
                     model.addAttribute("userinfo",user1);
                     return "/WEB-INF/jsp/showUser.jsp";
                 }else{
                   /*  List<User> userList = userService.getAllUser();
                     for(User user :userList){
                         if(userName.equals(user.getUserName())){
                             log.info(JSON.toJSONString(userList));
                             model.addAttribute("userinfo",user);
                             Cookie cookie1 = new Cookie(userName,FastJsonUtils.toJSONString(user) );
                             cookie1.setMaxAge(0);
                             Cookie cookie = new Cookie(userName, FastJsonUtils.toJSONString(user));
                             cookie.setMaxAge(7*24*60*60);     // 一星期有效
                             response.addCookie(cookie);
                             return "showUser";
                         }
                     }*/

                   return null;

                 }
            }
        }

        return null;
    }



}
